package br.com.rglariguet.rgportal.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import br.com.rglariguet.rgportal.Colaborador;
import br.com.rglariguet.rgportal.dao.ColaboradorDAO;

@Repository
@Transactional
public class ColaboradorDAOImpl implements ColaboradorDAO {

	private final SessionFactory factory;
	
	@Autowired
	public ColaboradorDAOImpl (SessionFactory factory){
		this.factory = factory;
	}
	
	private Session getSession() {
		return factory.getCurrentSession();
	}

	@Override
	public void salvar(Colaborador colaborador) {         				
		getSession().save(colaborador);  		
	}
	
	@Override
	public List<Colaborador> listarTodos() {
		return (List<Colaborador>)getSession().createQuery("from Colaborador as col order by col.nome").list();
	}

	@Override
	public Colaborador buscarPorId(Long id) {
		return (Colaborador) getSession().get(Colaborador.class, id);
	}
	
	@Override
	public void alterar(Colaborador colaborador) {
		getSession().update(colaborador);
	}

	@Override
	public void excluir(Colaborador colaborador) {
		getSession().delete(colaborador);
	}
	
}
